import { Scene } from '@babylonjs/core/scene';
import { WebXRSessionManager } from '@babylonjs/core/XR/webXRSessionManager';
import { WebXRDefaultExperienceOptions } from '@babylonjs/core/XR/webXRDefaultExperience';
import { IXRInputOptions } from './xr-input/xr-input';
import '@babylonjs/core/Materials/Node/Blocks';
import { IActiveDefaultXRExperienceOptions, IXRContainer, XRContainer } from './xr-container';


/**
 * xr-ar参数对象
 * @param inputOptions xr输入参数对象 
 */
interface IActiveDefaultARExperienceOptions extends IActiveDefaultXRExperienceOptions {
    inputOptions?: IXRInputOptions,
}

/**
 * 启用xr，生成ar容器实例
 * @param scene 所在场景
 * @param canvas 画布元素
 * @param options 参数对象
 * @returns 容器promise对象
 */
const ActiveDefaultARExperience = async (scene: Scene, canvas: HTMLCanvasElement, options?: IActiveDefaultARExperienceOptions): Promise<ARContainer | null> => {
    const ops = options || {};
    const ar = new ARContainer();
    await ar.ActiveDefaultXRExperience(scene, canvas, ops);
    return ar;
};

/**
 * ar容器接口
 */
interface IARContainer extends IXRContainer<ARContainer> {
}


/**
 * ar容器
 */
class ARContainer extends XRContainer<ARContainer> implements IARContainer {

    protected getXROptions(option: IActiveDefaultARExperienceOptions): WebXRDefaultExperienceOptions {
        const ops = super.getXROptions(option);
        ops.disableTeleportation = true;
        ops.inputOptions = {
            doNotLoadControllerMeshes: false,
            customControllersRepositoryURL: option.customControllersRepositoryURL,
        };
        return ops;
    }

    protected setLocomotion(scene: Scene, options: IActiveDefaultARExperienceOptions): void {
    }

    protected setMovement(scene: Scene, options: IActiveDefaultARExperienceOptions): void {
    }

    protected setTeleportation(scene: Scene, options: IActiveDefaultARExperienceOptions): void {
    }

    protected async supportImmersiveMode(): Promise<boolean> {
        return await WebXRSessionManager.IsSessionSupportedAsync('immersive-ar');
    }

}


export { ActiveDefaultARExperience, ARContainer };
export type { IActiveDefaultARExperienceOptions, IARContainer };